package adv410;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class XMLRead {
    public static void main(String[] args) {
        //read();
        //search();
        // search1();
       // search2();
        //add("孙洁",21,"男","11121","32222","java");
        //add("任明亮",23,"男","13111","222242","java高级");
        //del("赵俊杰");
        update("杨旭","13666666",22);
    }
    //根据姓名，改手机号和年龄
    public static void update(String name,String phone,int age){
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new FileInputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"));
           Element e =(Element) doc.selectSingleNode("/class/student[name='" + name + "']");
            if(e==null){
                System.out.println("查无此人");
                return;
            }
            e.addAttribute("telephone",phone);
            e.selectSingleNode("age").setText(String.valueOf(age));
            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw=new XMLWriter(new FileOutputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"),format);
            xw.write(doc);
            xw.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //删除学生,根据姓名
    public static void del(String name){
        //getParent()获得上级
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new FileInputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"));
            List<Element> list =doc.selectNodes("/class/student[name='" + name + "']");
            if(list.size()==0){
                System.out.println("查无此人");
                return;
            }
            for(Element e:list){
                e.getParent().remove(e);
            }
            System.out.println("删除成功");
            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw=new XMLWriter(new FileOutputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"),format);
            xw.write(doc);
            xw.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //新学生加入
    public static void add(String name,int age,String sex,String qq,String phone,String goodAt){
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new FileInputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"));
            Element root = doc.getRootElement();
            Element stu = root.addElement("student");
            stu.addAttribute("qq",qq);
            stu.addAttribute("telephone",phone);
            stu.addElement("name").setText(name);
            stu.addElement("sex").setText(sex);
            stu.addElement("age").setText(String.valueOf(age));
            stu.addElement("goodat").setText(goodAt);
            //格式
            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw=new XMLWriter(new FileOutputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
            "\\adv47\\java83.xml"),format);
           xw.write(doc);
            xw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //根据姓名查找学生信息
    public static void search2() {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入你要找的学生姓名：");
        String name2 = in.next();
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new FileInputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"));
            List<Element> list = doc.selectNodes("/class/student[name='" + name2 + "']");
            if (list.size() == 0) {
                System.out.println("没有这个手机号的人");
                return;
            }
            for (Element e : list) {
                //读取属性
                String qq = e.attributeValue("qq");
                String telphone = e.attributeValue("telephone");

                Element ename = (Element) e.selectSingleNode("name");
                String name = ename.getTextTrim();//去空格
                Element eage = (Element) e.selectSingleNode("age");
                int age = Integer.parseInt(eage.getTextTrim());

                Element esex = (Element) e.selectSingleNode("sex");
                String sex = esex.getTextTrim();//去空格
                Element egoodat = (Element) e.selectSingleNode("goodat");
                String goodat = egoodat.getTextTrim();//去空格

                System.out.printf("%s,age=%d,sex=%s,telephone=%s,擅长:%s,QQ=%s\n", name, age, sex, telphone, goodat, qq);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void search1() {
        //根据手机号，查找学生的信息
        Scanner in = new Scanner(System.in);
        System.out.print("请输入你要找的学生手机号：");
        String phone = in.next();
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new FileInputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"));
            List<Element> list = doc.selectNodes("/class/student[@telephone='" + phone + "']");
            if (list.size() == 0) {
                System.out.println("没有这个手机号的人");
                return;
            }
            for (Element e : list) {
                //读取属性
                String qq = e.attributeValue("qq");
                String telphone = e.attributeValue("telephone");
                if (!telphone.equals(phone)) {
                    continue;
                }
                Element ename = (Element) e.selectSingleNode("name");
                String name = ename.getTextTrim();//去空格
                Element eage = (Element) e.selectSingleNode("age");
                int age = Integer.parseInt(eage.getTextTrim());

                Element esex = (Element) e.selectSingleNode("sex");
                String sex = esex.getTextTrim();//去空格
                Element egoodat = (Element) e.selectSingleNode("goodat");
                String goodat = egoodat.getTextTrim();//去空格

                System.out.printf("%s,age=%d,sex=%s,telephone=%s,擅长:%s,QQ=%s\n", name, age, sex, telphone, goodat, qq);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    //麻烦了。。。
    public static void search() {
        //根据手机号，查找学生的信息
        Scanner in = new Scanner(System.in);
        System.out.print("请输入你要找的学生手机号：");
        String phone = in.next();
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new FileInputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"));
            Element root = doc.getRootElement();
            //>System.out.println("根节点是：" + root.getName());
            List<Element> list = root.selectNodes("student");
            for (Element e : list) {
                //读取属性
                String qq = e.attributeValue("qq");
                String telphone = e.attributeValue("telephone");
                if (!telphone.equals(phone)) {
                    continue;
                }
                Element ename = (Element) e.selectSingleNode("name");
                String name = ename.getTextTrim();//去空格
                Element eage = (Element) e.selectSingleNode("age");
                int age = Integer.parseInt(eage.getTextTrim());

                Element esex = (Element) e.selectSingleNode("sex");
                String sex = esex.getTextTrim();//去空格
                Element egoodat = (Element) e.selectSingleNode("goodat");
                String goodat = egoodat.getTextTrim();//去空格

                System.out.printf("%s,age=%d,sex=%s,telephone=%s,擅长:%s,QQ=%s\n", name, age, sex, telphone, goodat, qq);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void read() {
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new FileInputStream("E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src" +
                    "\\adv47\\java83.xml"));
            Element root = doc.getRootElement();
            System.out.println("根节点是：" + root.getName());
            List<Element> list = root.selectNodes("student");
            for (Element e : list) {
                //读取属性
                String qq = e.attributeValue("qq");
                String telphone = e.attributeValue("telephone");
                Element ename = (Element) e.selectSingleNode("name");
                String name = ename.getTextTrim();//去空格
                Element eage = (Element) e.selectSingleNode("age");
                int age = Integer.parseInt(eage.getTextTrim());

                Element esex = (Element) e.selectSingleNode("sex");
                String sex = esex.getTextTrim();//去空格
                Element egoodat = (Element) e.selectSingleNode("goodat");
                String goodat = egoodat.getTextTrim();//去空格

                System.out.printf("%s,age=%d,sex=%s,telephone=%s,擅长:%s,QQ=%s\n", name, age, sex, telphone, goodat, qq);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
